package com.jcl.recruit.user.mapper;

import com.jcl.recruit.user.model.User;
import com.jcl.core.utils.StringUtils;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class UserSqlProvider {

    public String insertSelective(User record) {
        BEGIN();
        INSERT_INTO("user");

        if (record.getId() != null) {
            VALUES("id", "#{id,jdbcType=INTEGER}");
        }
        if (record.getRealname() != null) {
            VALUES("realname", "#{realname,jdbcType=VARCHAR}");
        }
        
        if (record.getUsername() != null) {
            VALUES("username", "#{username,jdbcType=VARCHAR}");
        }
        
        if (record.getPassword() != null) {
            VALUES("password", "#{password,jdbcType=VARCHAR}");
        }
        
        if (record.getEmail() != null) {
            VALUES("email", "#{email,jdbcType=VARCHAR}");
        }
        
        if (record.getSubcompanyId() != null) {
            VALUES("subcompany_id", "#{subcompanyId,jdbcType=INTEGER}");
        }
        
        if (record.getSubcompany() != null) {
            VALUES("subcompany", "#{subcompany,jdbcType=VARCHAR}");
        }
        
        if (record.getDepartmentId() != null) {
            VALUES("department_id", "#{departmentId,jdbcType=INTEGER}");
        }
        
        if (record.getDepartment() != null) {
            VALUES("department", "#{department,jdbcType=VARCHAR}");
        }
        
        if (record.getWorkcode() != null) {
            VALUES("workcode", "#{workcode,jdbcType=VARCHAR}");
        }
        
        if (record.getIshr() != null) {
            VALUES("ishr", "#{ishr,jdbcType=BIT}");
        }

        if (StringUtils.hasLength(record.getRoles())) {
            VALUES("roles", "#{roles,jdbcType=VARCHAR}");
        }

        if (record.getCorpCode() != null) {
            VALUES("corp_code", "#{corpCode,jdbcType=VARCHAR}");
        }

        if (record.getStatus() != null) {
            VALUES("status", "#{status,jdbcType=INTEGER}");
        }
        
        if (record.getCreatetime() != null) {
            VALUES("createtime", "#{createtime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getToken() != null) {
            VALUES("token", "#{token,jdbcType=VARCHAR}");
        }
        
        return SQL();
    }

    public String updateByPrimaryKeySelective(User record) {
        BEGIN();
        UPDATE("user");
        
        if (record.getRealname() != null) {
            SET("realname = #{realname,jdbcType=VARCHAR}");
        }
        
        if (record.getUsername() != null) {
            SET("username = #{username,jdbcType=VARCHAR}");
        }
        
        if (record.getPassword() != null) {
            SET("password = #{password,jdbcType=VARCHAR}");
        }
        
        if (record.getEmail() != null) {
            SET("email = #{email,jdbcType=VARCHAR}");
        }
        
        if (record.getSubcompanyId() != null) {
            SET("subcompany_id = #{subcompanyId,jdbcType=INTEGER}");
        }
        
        if (record.getSubcompany() != null) {
            SET("subcompany = #{subcompany,jdbcType=VARCHAR}");
        }
        
        if (record.getDepartmentId() != null) {
            SET("department_id = #{departmentId,jdbcType=INTEGER}");
        }
        
        if (record.getDepartment() != null) {
            SET("department = #{department,jdbcType=VARCHAR}");
        }
        
        if (record.getWorkcode() != null) {
            SET("workcode = #{workcode,jdbcType=VARCHAR}");
        }
        
        if (record.getIshr() != null) {
            SET("ishr = #{ishr,jdbcType=BIT}");
        }

        if (StringUtils.hasLength(record.getRoles())) {
            SET("roles= #{roles,jdbcType=VARCHAR}");
        }

        if (record.getCreatetime() != null) {
            SET("createtime = #{createtime,jdbcType=TIMESTAMP}");
        }
        if (record.getStatus() != null) {
            SET("status = #{status,jdbcType=INTEGER}");
        }

        if (record.getToken() != null) {
            SET("token = #{token,jdbcType=VARCHAR}");
        }
        
        WHERE("id = #{id,jdbcType=INTEGER}");
        
        return SQL();
    }
}